using System.Collections.Generic;
using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using Game.XH;
using UnityEngine;

namespace Game.Component.Operation
{
	public class Game4Component : OperationComponent
	{
		protected float MIN_X = -1.5f;

		protected float MAX_X = 1.43f;

		protected float MIN_Y = -2.3f;

		protected float MAX_Y = 3.1f;

		private Transform btnBack;

		private Transform touchTile;

		private Transform targetTileBox;

		private Vector2 lastTouchPoint;

		private Transform efcComplete;

		private List<Transform> efcs = new List<Transform>();

		private List<Transform> completeTiles = new List<Transform>();

		private float newX;

		private float newY;

		private void Awake()
		{
			Init();
			btnBack = base.transform.Find("BtnBack");
			int childCount = base.transform.childCount;
			for (int i = 0; i < childCount; i++)
			{
				Transform child = base.transform.GetChild(i);
				if (child.name.Contains("Tile") && child.name.Length < 6)
				{
					child.SetLocalXY(Random.Range(MIN_X, MAX_X), Random.Range(MIN_Y, MAX_Y));
				}
			}
		}

		protected virtual void Init()
		{
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			if (displayer == null)
			{
				return;
			}
			base.TouchDown(touchEventData);
			RaycastHit2D raycastHit2D = Physics2D.Raycast(touchEventData.curPos, Vector2.zero, 15f, 1);
			touchTile = null;
			targetTileBox = null;
			if (raycastHit2D.collider != null)
			{
				if (raycastHit2D.collider.transform == btnBack)
				{
					Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
					displayer.ChangeType(OperationDisplayerItem.Type.Main);
				}
				else if (raycastHit2D.collider.transform.name.Contains("Tile") && !completeTiles.Contains(raycastHit2D.collider.transform))
				{
					targetTileBox = base.transform.Find(raycastHit2D.collider.transform.name + "Box");
					if (targetTileBox != null)
					{
						touchTile = raycastHit2D.collider.transform;
					}
				}
			}
			lastTouchPoint = touchEventData.curPos;
		}

		public override void TouchMove(TouchEventData touchEventData)
		{
			base.TouchMove(touchEventData);
			if (touchTile != null)
			{
				newX = touchTile.localPosition.x + touchEventData.curPos.x - lastTouchPoint.x;
				newY = touchTile.localPosition.y + touchEventData.curPos.y - lastTouchPoint.y;
				if (newX < MIN_X)
				{
					newX = MIN_X;
				}
				else if (newX > MAX_X)
				{
					newX = MAX_X;
				}
				if (newY < MIN_Y)
				{
					newY = MIN_Y;
				}
				else if (newY > MAX_Y)
				{
					newY = MAX_Y;
				}
				if (Mathf.Abs(newX - targetTileBox.localPosition.x) < 0.1f && Mathf.Abs(newY - targetTileBox.localPosition.y) < 0.1f)
				{
					newX = targetTileBox.localPosition.x;
					newY = targetTileBox.localPosition.y;
					touchTile.SetLocalXY(newX, newY);
					Singleton<AudioManager>.Instance.PlaySound("sound_embed");
					Singleton<AudioManager>.Instance.PlaySound("sound_blink");
					Transform transform = Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_blink_1", touchTile.transform.position + new Vector3(0f, 0f, -1E-05f), 3f);
					transform.SetParent(base.transform);
					efcs.Add(transform);
					completeTiles.Add(touchTile);
					touchTile.GetComponent<BoxCollider2D>().enabled = false;
					touchTile = null;
					targetTileBox = null;
				}
				else
				{
					touchTile.SetLocalXY(newX, newY);
				}
			}
			lastTouchPoint = touchEventData.curPos;
		}

		private void OnDestroy()
		{
			for (int i = 0; i < efcs.Count; i++)
			{
				if (efcs[i] != null)
				{
					Singleton<GamePoolManager>.Instance.Despawn(efcs[i]);
				}
			}
			efcs.Clear();
		}
	}
}
